package com.mlethe.android.demo.seattable.base;

import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

public abstract class Activity<P extends BasePresenter, V extends IView> extends android.app.Activity implements LifecycleOwner {

    private LifecycleRegistry mLifecycleRegistry;

    protected P iPresenter;

    public Activity() {
        this.mLifecycleRegistry = new LifecycleRegistry(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLifecycleRegistry.markState(LifecycleRegistry.State.CREATED);
        // 全屏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        hideBottomUIMenu();
        iPresenter = createPresenter();
        if (iPresenter != null) {
            iPresenter.attachView((V)this);
            iPresenter.onCreate();
        }

        // 设置布局layout
        setContentView();

        // 初始化头部
        initTitle();

        // view与数据绑定
        initView();

        // 初始化数据
        initData();
    }

    /**
     * 隐藏虚拟按键
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    protected abstract P createPresenter();

    /**
     * 设置布局layout
     */
    protected abstract void setContentView();

    /**
     * 初始化头部
     */
    protected abstract void initTitle();

    /**
     * view与数据绑定
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    @Override
    protected void onStart() {
        super.onStart();
        mLifecycleRegistry.markState(LifecycleRegistry.State.STARTED);
        if (iPresenter != null) {
            iPresenter.onStart();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mLifecycleRegistry.markState(LifecycleRegistry.State.RESUMED);
        if (iPresenter != null) {
            iPresenter.onResume();
        }
    }

    @Override
    protected void onPause() {
        if (iPresenter != null) {
            iPresenter.onPause();
        }
        super.onPause();
        mLifecycleRegistry.markState(LifecycleRegistry.State.STARTED);
    }

    @Override
    protected void onStop() {
        if (iPresenter != null) {
            iPresenter.onStop();
        }
        super.onStop();
        mLifecycleRegistry.markState(LifecycleRegistry.State.CREATED);
    }

    @Override
    protected void onDestroy() {
        if (iPresenter != null) {
            iPresenter.onDestroy();
        }
        super.onDestroy();
        mLifecycleRegistry.markState(LifecycleRegistry.State.DESTROYED);
    }

    @NonNull
    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }
}
